$.reqcount = 0; $.reqerror = 0; $.ajaxpopup = 0;
//$.ajaxSetup({ scriptCharset: "utf-8" , contentType: "application/javascript; charset=utf-8"});

function ajax_open_popup(code, width) {
	if ($.ajaxpopup>0) {
		return ajax_hide_popup(function() {
			ajax_open_popup(code, width);
		});
	}
	if ($.ajaxpopup==-1) { $('#ajax-popup').stop().css('top', '-1000px'); }
	$.ajaxpopup = 1;
	if (width!=0) $('#ajax-popup-content').css('width', width + 'px');
	else $('#ajax-popup-content').css('width', '500px');
	$('#ajax-popup').css('top', '-1000px');
	$('#ajax-popup-content').html(code);
	$('#ajax-popup').css('top', '-'+ $('#ajax-popup-content').outerHeight() +'px');
	$('#ajax-popup').stop().animate({top:'0px'},400,'swing',function() { $.ajaxpopup = 2; });
}

function ajax_hide_popup(oncomplete) {
	$.ajaxpopup = -1;
	$('#ajax-popup').stop().animate({top:'-'+ $('#ajax-popup-content').outerHeight() +'px'},400,'swing',function() { $.ajaxpopup = 0; if(oncomplete) oncomplete(); });
}

$(document).ready(function() {
	$('#ajax-working').bind('ajaxreq', function() {
		if ($.reqcount==0) {
			if (!$.reqerror) {
				$('#ajax-working').stop().animate({top:'-65px'});
			}else{
				$('#ajax-working').html('error').css('background', '#aa1122').stop();
				setTimeout("$('#ajax-working').animate({top:'-65px'});", 1500);
			}
		}else{
			$('#ajax-working').stop().animate({top:'0px'});
		}
	})
});

function minf_ajax(app, call, args) { 
	var cvargs;
	$.reqcount++;	// ACTIVE REQUEST COUNT
	$('#ajax-working').trigger('ajaxreq');
	cvargs = serialize(getArgs(args));
	$.ajax({
		type: "POST",
		url: $.rooturl + "/ajax:" + app + "/" + call,
		//contentType: "application/javascript; charset=utf-8\",
		dataType: "script",
		data: {app: app, call: call, args: cvargs},
		success: function () {
			$.reqcount--;
			$('#ajax-working').trigger('ajaxreq');
			$(document).trigger('ready');
			},
		error: function () {
			$.reqcount--;
			$.reqerror++;
			$('#ajax-working').trigger('ajaxreq');
			$(document).trigger('ready');
		}
	});
}

function getArgs(args) {
	var value;
	var argsret = [];
	for (var i=0;i<args.length;i++)
//		argsret.push(encodeURIComponent(args[i]));
		argsret.push(Base64.encode(args[i]));
	return argsret;
}

function serialize(mixed_value) {
	    // Generates a storable representation of a value
	    // 
	    // +    discuss at: http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_serialize/
	    // +       version: 812.3015
	    // +   original by: Arpad Ray (mailto:arpad@php.net)
	    // +   improved by: Dino
	    // +   bugfixed by: Andrej Pavlovic
	    // +   bugfixed by: Garagoth
	    // %          note: We feel the main purpose of this function should be to ease the transport of data between php & js
	    // %          note: Aiming for PHP-compatibility, we have to translate objects to arrays
	    // *     example 1: serialize(['Kevin', 'van', 'Zonneveld']);
	    // *     returns 1: 'a:3:{i:0;s:5:"Kevin";i:1;s:3:"van";i:2;s:9:"Zonneveld";}'
	    // *     example 2: serialize({firstName: 'Kevin', midName: 'van', surName: 'Zonneveld'});
	    // *     returns 2: 'a:3:{s:9:"firstName";s:5:"Kevin";s:7:"midName";s:3:"van";s:7:"surName";s:9:"Zonneveld";}'

	    var _getType = function( inp ) {
	        var type = typeof inp, match;
	        var key;
	        if (type == 'object' && !inp) {
	            return 'null';
	        }
	        if (type == "object") {
	            if (!inp.constructor) {
	                return 'object';
	            }
	            var cons = inp.constructor.toString();
	            if (match = cons.match(/(\w+)\(/)) {
	                cons = match[1].toLowerCase();
	            }
	            var types = ["boolean", "number", "string", "array"];
	            for (key in types) {
	                if (cons == types[key]) {
	                    type = types[key];
	                    break;
	                }
	            }
	        }
	        return type;
	    };
	    var type = _getType(mixed_value);
	    var val, ktype = '';

	    switch (type) {
	        case "function": 
	            val = ""; 
	            break;
	        case "undefined":
	            val = "N";
	            break;
	        case "boolean":
	            val = "b:" + (mixed_value ? "1" : "0");
	            break;
	        case "number":
	            val = (Math.round(mixed_value) == mixed_value ? "i" : "d") + ":" + mixed_value;
	            break;
	        case "string":
	            val = "s:" + mixed_value.length + ":\"" + mixed_value + "\"";
	            break;
	        case "array":
	        case "object":
	            val = "a";
	            /*
	            if (type == "object") {
	                var objname = mixed_value.constructor.toString().match(/(\w+)\(\)/);
	                if (objname == undefined) {
	                    return;
	                }
	                objname[1] = serialize(objname[1]);
	                val = "O" + objname[1].substring(1, objname[1].length - 1);
	            }
	            */
	            var count = 0;
	            var vals = "";
	            var okey;
	            var key;
	            for (key in mixed_value) {
	                ktype = _getType(mixed_value[key]);
	                if (ktype == "function") { 
	                    continue; 
	                }

	                okey = (key.match(/^[0-9]+$/) ? parseInt(key) : key);
	                vals += serialize(okey) +
	                        serialize(mixed_value[key]);
	                count++;
	            }
	            val += ":" + count + ":{" + vals + "}";
	            break;
	    }
	    if (type != "object" && type != "array") val += ";";
	    return val;
}

function unserialize(data){
    // Creates a PHP value from a stored representation
    // 
    // +    discuss at: http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_unserialize/
    // +       version: 903.421
    // +     original by: Arpad Ray (mailto:arpad@php.net)
    // +     improved by: Pedro Tainha (http://www.pedrotainha.com)
    // +     bugfixed by: dptr1988
    // +      revised by: d3x
    // +     improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: Brett Zamir
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // %            note: We feel the main purpose of this function should be to ease the transport of data between php & js
    // %            note: Aiming for PHP-compatibility, we have to translate objects to arrays 
    // *       example 1: unserialize('a:3:{i:0;s:5:"Kevin";i:1;s:3:"van";i:2;s:9:"Zonneveld";}');
    // *       returns 1: ['Kevin', 'van', 'Zonneveld']
    // *       example 2: unserialize('a:3:{s:9:"firstName";s:5:"Kevin";s:7:"midName";s:3:"van";s:7:"surName";s:9:"Zonneveld";}');
    // *       returns 2: {firstName: 'Kevin', midName: 'van', surName: 'Zonneveld'}
    
    var error = function (type, msg, filename, line){throw new window[type](msg, filename, line);};
    var read_until = function (data, offset, stopchr){
        var buf = [];
        var chr = data.slice(offset, offset + 1);
        var i = 2;
        while (chr != stopchr) {
            if ((i+offset) > data.length) {
                error('Error', 'Invalid');
            }
            buf.push(chr);
            chr = data.slice(offset + (i - 1),offset + i);
            i += 1;
        }
        return [buf.length, buf.join('')];
    };
    var read_chrs = function (data, offset, length){
        var buf;
        
        buf = [];
        for(var i = 0;i < length;i++){
            var chr = data.slice(offset + (i - 1),offset + i);
            buf.push(chr);
        }
        return [buf.length, buf.join('')];
    };
    var _unserialize = function (data, offset){
        var readdata;
        var readData;
        var chrs = 0;
        var ccount;
        var stringlength;
        var keyandchrs;
        var keys;

        if(!offset) offset = 0;
        var dtype = (data.slice(offset, offset + 1)).toLowerCase();
        
        var dataoffset = offset + 2;
        var typeconvert = new Function('x', 'return x');
        
        switch(dtype){
            case "i":
                typeconvert = new Function('x', 'return parseInt(x)');
                readData = read_until(data, dataoffset, ';');
                chrs = readData[0];
                readdata = readData[1];
                dataoffset += chrs + 1;
            break;
            case "b":
                typeconvert = new Function('x', 'return (parseInt(x) == 1)');
                readData = read_until(data, dataoffset, ';');
                chrs = readData[0];
                readdata = readData[1];
                dataoffset += chrs + 1;
            break;
            case "d":
                typeconvert = new Function('x', 'return parseFloat(x)');
                readData = read_until(data, dataoffset, ';');
                chrs = readData[0];
                readdata = readData[1];
                dataoffset += chrs + 1;
            break;
            case "n":
                readdata = null;
            break;
            case "s":
                ccount = read_until(data, dataoffset, ':');
                chrs = ccount[0];
                stringlength = ccount[1];
                dataoffset += chrs + 2;
                
                readData = read_chrs(data, dataoffset+1, parseInt(stringlength));
                chrs = readData[0];
                readdata = readData[1];
                dataoffset += chrs + 2;
                if(chrs != parseInt(stringlength) && chrs != readdata.length){
                    error('SyntaxError', 'String length mismatch');
                }
            break;
            case "a":
                readdata = {};
                
                keyandchrs = read_until(data, dataoffset, ':');
                chrs = keyandchrs[0];
                keys = keyandchrs[1];
                dataoffset += chrs + 2;
                
                for(var i = 0;i < parseInt(keys);i++){
                    var kprops = _unserialize(data, dataoffset);
                    var kchrs = kprops[1];
                    var key = kprops[2];
                    dataoffset += kchrs;
                    
                    var vprops = _unserialize(data, dataoffset);
                    var vchrs = vprops[1];
                    var value = vprops[2];
                    dataoffset += vchrs;
                    
                    readdata[key] = value;
                }
                
                dataoffset += 1;
            break;
            default:
                error('SyntaxError', 'Unknown / Unhandled data type(s): ' + dtype);
            break;
        }
        return [dtype, dataoffset - offset, typeconvert(readdata)];
    };
    return _unserialize(data, 0)[2];
}

/**
*
*  Base64 encode / decode
*  http://www.webtoolkit.info/
*
**/
 
var Base64 = {
 
	// private property
	_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
 
	// public method for encoding
	encode : function (input) {
		var output = "";
		var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
		var i = 0;
 
		input = Base64._utf8_encode(input);
 
		while (i < input.length) {
 
			chr1 = input.charCodeAt(i++);
			chr2 = input.charCodeAt(i++);
			chr3 = input.charCodeAt(i++);
 
			enc1 = chr1 >> 2;
			enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
			enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
			enc4 = chr3 & 63;
 
			if (isNaN(chr2)) {
				enc3 = enc4 = 64;
			} else if (isNaN(chr3)) {
				enc4 = 64;
			}
 
			output = output +
			this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
			this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
 
		}
 
		return output;
	},
 
	// public method for decoding
	decode : function (input) {
		var output = "";
		var chr1, chr2, chr3;
		var enc1, enc2, enc3, enc4;
		var i = 0;
 		if (input===undefined) input = "";
		input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
 
		while (i < input.length) {
 
			enc1 = this._keyStr.indexOf(input.charAt(i++));
			enc2 = this._keyStr.indexOf(input.charAt(i++));
			enc3 = this._keyStr.indexOf(input.charAt(i++));
			enc4 = this._keyStr.indexOf(input.charAt(i++));
 
			chr1 = (enc1 << 2) | (enc2 >> 4);
			chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
			chr3 = ((enc3 & 3) << 6) | enc4;
 
			output = output + String.fromCharCode(chr1);
 
			if (enc3 != 64) {
				output = output + String.fromCharCode(chr2);
			}
			if (enc4 != 64) {
				output = output + String.fromCharCode(chr3);
			}
 
		}
 
		output = Base64._utf8_decode(output);
 
		return output;
 
	},
 
	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string += ''; 
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
 
		for (var n = 0; n < string.length; n++) {
 
			var c = string.charCodeAt(n);
 
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
 
		}
 
		return utftext;
	},
 
	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
 
		while ( i < utftext.length ) {
 
			c = utftext.charCodeAt(i);
 
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
 
		}
 
		return string;
	}
 
}

function is_array( mixed_var ) {
    // Returns true if variable is an array  
    // 
    // version: 901.1623
    // discuss at: http://phpjs.org/functions/is_array
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Legaev Andrey
    // +   bugfixed by: Cord
    // +   bugfixed by: Manish
    // +   improved by: Onno Marsman
    // %        note 1: In php.js, javascript objects are like php associative arrays, thus JavaScript objects will also
    // %        note 1: return true
    // *     example 1: is_array(['Kevin', 'van', 'Zonneveld']);
    // *     returns 1: true
    // *     example 2: is_array('Kevin van Zonneveld');
    // *     returns 2: false
    // *     example 3: is_array({0: 'Kevin', 1: 'van', 2: 'Zonneveld'});
    // *     returns 3: true
    // *     example 4: is_array(function tmp_a(){this.name = 'Kevin'});
    // *     returns 4: false
    var key = '';

    if (!mixed_var) {
        return false;
    }

    if (typeof mixed_var === 'object') {

        if (mixed_var.hasOwnProperty) {
            for (key in mixed_var) {
                // Checks whether the object has the specified property
                // if not, we figure it's not an object in the sense of a php-associative-array.
                if (false === mixed_var.hasOwnProperty(key)) {
                    return false;
                }
            }
        }

        // Uncomment to enable strict JavsScript-proof type checking
        // This will not support PHP associative arrays (JavaScript objects), however
        // Read discussion at: http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_is_array/
        //
        //  if (mixed_var.propertyIsEnumerable('length') || typeof mixed_var.length !== 'number') {
        //      return false;
        //  }

        return true;
    }

    return false;
}

function getVals(items) {
	var vals = [];
	if (items) {
		items.each(function() {
			vals.push($(this).val());
		});
	}
	return vals;
}

function getVals64(items) {
	var vals = [];
	if (items) {
		items.each(function() {
			vals.push(Base64.encode($(this).val()));
		});
	}
	return vals;
}